﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AttributeRouting.Web.Mvc;
using CobraCms.AdminConsole.Core.Entities;
using CobraCms.AdminConsole.Web.Models.ContentTree;
using CobraCms.Core.Content.Interfaces;
using CobraCms.Core.Content.Utilities;
using System.Threading;

namespace CobraCms.AdminConsole.Web.Controllers.Content
{
    public class ContentTreeController : Controller
    {
        private IContentRepository _contentRepository;


        public ContentTreeController(IContentRepository contentRepository)
        {
            _contentRepository = contentRepository;
        }


        [GET("/ajax/content/tree/nodes/root")]
        public ActionResult Root()
        {
            var rootNodes = _contentRepository.SearchContentNodes(SearchCriteriaUtilities.RootNodes());
            var rootNodeModel = new ContentTreeNodeModel();
            rootNodeModel.Name = "Content";

            foreach (var rootNode in rootNodes)
            {
                var model = new ContentTreeNodeModel();
                model.NodeId = rootNode.Id;
                model.Name = rootNode.Name;
                model.Actions = GetActions(rootNode.Name);
                rootNodeModel.PreExpandedChildren.Add(model);
            }             
            return Json(rootNodeModel, JsonRequestBehavior.AllowGet);
        }

        [GET("/ajax/content/tree/nodes/{nodeId}/children")]
        public ActionResult NodeChildren(string nodeId)
        {
            var childNodes = _contentRepository.SearchContentNodes(SearchCriteriaUtilities.Children(nodeId));
            
            var childModels = new List<ContentTreeNodeModel>();

            foreach (var node in childNodes)
            {
                var model = new ContentTreeNodeModel();
                model.NodeId = node.Id;
                model.Name = node.Name;
                model.Actions = GetActions(node.Name);
                childModels.Add(model);
            }
            return Json(childModels, JsonRequestBehavior.AllowGet);
        }

        private List<ContentTreeNodeAction> GetActions(string name)
        {
            var actions = new List<ContentTreeNodeAction>();
            actions.Add(new ContentTreeNodeAction
                            {
                                ActionKey = "Create",
                                Name = "Add Child",
                                Description = "Add a piece of content under this node",
                                Enabled = true                                
                            });
            actions.Add(new ContentTreeNodeAction
            {
                ActionKey = "Move",
                Name = "Move Node",
                Description = "Moves this node to another place in the tree",
                Enabled = true
            });
            if (name != "Home")
            {
                actions.Add(new ContentTreeNodeAction
                {
                    ActionKey = "Delete",
                    Name = "Delete Node",
                    Description = "Deletes this node completely",
                    Enabled = true
                });
            }
            actions.Add(new ContentTreeNodeAction
            {
                ActionKey = "Refresh",
                Name = "Refresh node",
                Description = "Reloads the tree",
                Enabled = true
            });
            return actions;
        }

        

    }
}
